from django.db import models
from user.models import User, FileStore
from storehouse.models import Storehouse
from user.models import Company


# Create your models here.
class SoftwareType(models.Model):
    SOFTWARETYPESTATE = (
        ('0', u'已关闭'),
        ('1', u'已开启'),
    )
    softwareType = models.CharField(
        verbose_name=u'软件类型',
        default=u'',
        max_length=20
    )
    softwareTypeState = models.CharField(
        verbose_name=u'新增软件分类',
        choices=SOFTWARETYPESTATE,
        max_length=50,
        default=1,
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )

    def __unicode__(self):
        return self.softwareType

    class Meta:
        ordering = ('createTime',)


class Software(models.Model):
    # EXAMINESTATE = (
    #     ('0', u'等待审核'),
    #     ('1', u'审核通过'),
    #     ('2', u'审核未通过'),
    # )
    # SOFTWARESTATE = (
    #     ('0', u'已上传'),
    #     ('1', u'已归档'),
    # )
    user = models.ForeignKey(
        User,
        verbose_name=u"上传者",
        on_delete=models.CASCADE,
        related_name='software_uploader',
    )
    softwareId = models.CharField(
        max_length=30,
        verbose_name=u'软件id',
        default=u'',
    )
    softwareDetails = models.CharField(
        max_length=2000,
        verbose_name=u'软件描述',
        default=u'',
    )
    # updateDetails = models.CharField(
    #     max_length=2000,
    #     verbose_name=u'更新描述',
    #     default=u'',
    #     null=True
    # )
    # uploadTime = models.DateTimeField(
    #     auto_now_add=True,
    #     verbose_name=u"上传时间",
    # )
    # isExamine = models.BooleanField(
    #     verbose_name=u'是否审核',
    #     default=False,
    # )
    # store = models.ForeignKey(
    #     Storehouse,
    #     verbose_name=u"存储仓库",
    #     on_delete=models.CASCADE,
    #     related_name='software_store',
    #     null=True
    # )  # 一对多
    # softwareType = models.ForeignKey(
    #     SoftwareType,
    #     verbose_name=u"软件类型",
    #     on_delete=models.CASCADE,
    #     related_name='software_type',
    # )
    # softwareName = models.CharField(
    #     max_length=30,
    #     verbose_name=u'软件名称',
    #     default=u'',
    # )
    # designUnit = models.ForeignKey(
    #     Company,
    #     max_length=30,
    #     verbose_name=u'研发单位',
    #     on_delete=models.CASCADE,
    #     related_name='software_designCompany',
    # )
    # cover = models.ForeignKey(
    #     FileStore,
    #     verbose_name=u'封面文件',
    #     on_delete=models.CASCADE,
    #     related_name='cover_file',
    #     null=True
    # )
    # logo = models.ForeignKey(
    #     FileStore,
    #     verbose_name=u'logo文件',
    #     on_delete=models.CASCADE,
    #     related_name='logo_file',
    #     null=True
    # )
    isOwn = models.BooleanField(
        verbose_name=u'是否是本仓库软件',
        default=True,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )

    def __unicode__(self):
        return self.softwareName

    class Meta:
        ordering = ('createTime',)


class SoftwareVersion(models.Model):
    EXAMINESTATE = (
        ('0', u'等待审核'),
        ('1', u'审核通过'),
        ('2', u'审核未通过'),
    )
    SOFTWARESTATE = (
        ('0', u'已上传'),
        ('1', u'已归档'),
    )
    DELIVERYTYPE = (
        ('0', u'下载'),
        ('1', u'光盘'),
        ('2', u'硬盘'),
        ('3', u'自由下载'),
    )
    user = models.ForeignKey(
        User,
        verbose_name=u"上传者",
        on_delete=models.CASCADE,
        related_name='softwareVersion_uploader',
    )
    store = models.ForeignKey(
        Storehouse,
        verbose_name=u"存储仓库",
        on_delete=models.CASCADE,
        related_name='software_store',
        null=True
    )  # 一对多
    softwareType = models.ForeignKey(
        SoftwareType,
        verbose_name=u"软件类型",
        on_delete=models.CASCADE,
        related_name='software_type',
        null=True
    )
    softwareName = models.CharField(
        max_length=30,
        verbose_name=u'软件名称',
        default=u'',
    )
    designUnit = models.ForeignKey(
        Company,
        max_length=30,
        verbose_name=u'研发单位',
        on_delete=models.CASCADE,
        related_name='software_designCompany',
        null=True
    )
    cover = models.ForeignKey(
        FileStore,
        verbose_name=u'封面文件',
        on_delete=models.CASCADE,
        related_name='cover_file',
        null=True
    )
    logo = models.ForeignKey(
        FileStore,
        verbose_name=u'logo文件',
        on_delete=models.CASCADE,
        related_name='logo_file',
        null=True
    )
    software = models.ForeignKey(
        Software,
        verbose_name=u'所属软件',
        on_delete=models.CASCADE,
        related_name='software_version',
        null=True
    )
    softwareVersionId = models.CharField(
        max_length=30,
        verbose_name=u'软件版本id',
        default=u'',
    )
    file = models.ManyToManyField(
        FileStore,
        verbose_name=u'软件包',
        # on_delete=models.CASCADE,
        related_name='software_file',
        null=True
    )
    verification = models.ForeignKey(
        FileStore,
        verbose_name=u'查杀文件',
        on_delete=models.CASCADE,
        related_name='verification_file',
        null=True
    )
    qaFile = models.ManyToManyField(
        FileStore,
        verbose_name=u'QA文件',
        # on_delete=models.CASCADE,
        related_name='qaFile_file',
        null=True
    )
    cost = models.CharField(
        max_length=70,
        verbose_name=u'计费',
        default=u'',
    )
    version = models.CharField(
        max_length=30,
        verbose_name=u'版本号',
        default=u'',
    )
    updateDetails = models.CharField(
        max_length=2000,
        verbose_name=u'更新描述',
        default=u'',
        null=True
    )
    currentVersionDesc = models.CharField(
        max_length=2000,
        verbose_name=u"当前版本描述",
        default=u"",
    )
    examineState = models.CharField(
        verbose_name=u'软件审核状态',
        choices=EXAMINESTATE,
        max_length=20,
        default=0,
    )
    softwareState = models.CharField(
        verbose_name=u'新上传软件',
        choices=SOFTWARESTATE,
        max_length=20,
        default=0,
    )
    softwareResult = models.CharField(
        max_length=3000,
        verbose_name=u'软件处理意见',
        default=0,
        null=True
    )
    uploadTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"上传时间",
    )
    isExamine = models.BooleanField(
        verbose_name=u'是否审核',
        default=False,
    )
    isApply = models.BooleanField(
        verbose_name=u'软件是否开放权限',
        default=False,
    )
    isOnline = models.BooleanField(
        verbose_name=u'是否上线',
        default=False
    )
    deliveryType = models.CharField(
        max_length=20,
        verbose_name=u'配送方式',
        choices=DELIVERYTYPE,
        default=u'',
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )
    isDownload = models.ManyToManyField(
        Company,
        verbose_name=u"可下载单位",
        # on_delete=models.CASCADE,
        related_name='company_isDownload_softwareVersion',
        # null=True
    )
    isBrowse = models.ManyToManyField(
        Company,
        verbose_name=u"可浏览单位",
        # on_delete=models.CASCADE,
        related_name='company_isBrowse_softwareVersion',
        # null=True
    )
    isSeg = models.BooleanField(
        verbose_name=u'是否段包',
        default=False,
    )

    def __unicode__(self):
        return self.createTime

    class Meta:
        ordering = ('createTime',)


# 资料
class SoftwareData(models.Model):
    CHECKSTATE = (
        ('0', u'等待审核'),
        ('1', u'审核通过'),
        ('2', u'审核未通过'),
    )
    DATASTATE = (
        ('0', u'已上传'),
        ('1', u'已归档'),
    )
    # DATARESULT = (
    #     ('0', u'待审核'),
    #     ('1', u'审核通过'),
    #     ('2', u'已驳回'),
    # )
    softwareVersion = models.ManyToManyField(
        SoftwareVersion,
        verbose_name=u"软件版本",
        # on_delete=models.CASCADE,
        related_name='softwareVersion_data',
    )
    file = models.ForeignKey(
        FileStore,
        verbose_name=u'资料包',
        on_delete=models.CASCADE,
        related_name='data_file',
        null=True
    )
    dataName = models.CharField(
        max_length=30,
        verbose_name=u'资料名称',
        default=u'',
    )
    softwareFunction = models.CharField(
        max_length=30,
        verbose_name=u'软件描述',
        default=u'',
    )
    dataVersion = models.CharField(
        max_length=70,
        verbose_name=u'资料版本',
        default=u'',
    )
    dataCheckState = models.CharField(
        verbose_name=u'新增资料',
        choices=CHECKSTATE,
        max_length=20,
        default=0,
    )
    dataState = models.CharField(
        verbose_name=u'已上传资料',
        choices=DATASTATE,
        max_length=20,
        default=0,
    )
    dataResult = models.CharField(
        max_length=700,
        verbose_name=u'资料处理结果',
        default=u'',
        null=True
    )
    updateDetails = models.CharField(
        max_length=70,
        verbose_name=u'更新描述',
        default=u'',
        null=True
    )
    uploadTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"上传时间",
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )

    def __unicode__(self):
        return self.dataName

    class Meta:
        ordering = ('createTime',)


# 软件收藏
class SoftwareCollection(models.Model):
    software = models.ForeignKey(
        SoftwareVersion,
        verbose_name=u"软件",
        on_delete=models.CASCADE,
    )
    user = models.ForeignKey(
        User,
        verbose_name=u"用户",
        on_delete=models.CASCADE,
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )

    def __unicode__(self):
        return self.software

    class Meta:
        ordering = ('createTime',)


# 软件编排
class SoftwareGroup(models.Model):
    GROUPTYPE = (
        ('0', u'自由编排'),
        ('1', u'能力分组'),
        ('2', u'能力预案'),
        ('3', u'用户订阅'),
    )
    GROUPUSERTYPE = (
        ('1', u'开发人员'),
        ('2', u'管理人员'),
        ('3', u'使用人员'),
    )
    software = models.ManyToManyField(
        SoftwareVersion,
        related_name="software_arrange",
    )
    user = models.ForeignKey(
        User,
        verbose_name=u"用户",
        on_delete=models.CASCADE,
        related_name='group_user',
        null=True
    )
    groupName = models.CharField(
        max_length=70,
        verbose_name=u'编排名称',
        default=u'',
    )
    groupType = models.CharField(
        verbose_name=u'编排方式',
        choices=GROUPTYPE,
        max_length=20,
        default='0',
    )
    groupUserType = models.CharField(
        verbose_name=u'用户类型编排',
        choices=GROUPUSERTYPE,
        max_length=20,
        default='0',
    )
    describe = models.CharField(
        max_length=70,
        verbose_name=u'组描述',
        default=u'',
    )
    uploadTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"上传时间",
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )
    isOnline = models.BooleanField(
        verbose_name=u'是否上线',
        default=False
    )
    planName = models.CharField(
        verbose_name=u'方案名称',
        max_length=100,
        default=u'',
    )
    ratio = models.CharField(
        verbose_name=u'灰度测试',
        max_length=2000,
        default=u''
    )
    planContent = models.CharField(
        verbose_name=u'方案内容',
        max_length=100,
        default=u'',
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )
    # isDownload = models.ManyToManyField(
    #     Company,
    #     verbose_name=u"可下载单位",
    #     # on_delete=models.CASCADE,
    #     related_name='company_isDownload_softwareGroup',
    #     # null=True
    # )
    isBrowse = models.ManyToManyField(
        Company,
        verbose_name=u"可浏览单位",
        # on_delete=models.CASCADE,
        related_name='company_isBrowse_softwareGroup',
        # null=True
    )

    def __unicode__(self):
        return self.groupName

    class Meta:
        ordering = ('createTime',)


class DuanInfo(models.Model):
    realteId = models.CharField(
        max_length=500,
        verbose_name=u'依赖json',
        default=u'',
    )

    softwareVersion = models.ForeignKey(
        SoftwareVersion,
        verbose_name=u'所属软件版本',
        on_delete=models.CASCADE,
        related_name='softwareVersion_duanInfo',
        null=True
    )

    file = models.ForeignKey(
        FileStore,
        verbose_name=u'duan包',
        on_delete=models.CASCADE,
        related_name='duanInfo_file',
        null=True
    )
    type = models.CharField(
        max_length=30,
        verbose_name=u'zip类型',
        default=u'',
    )
    duanName = models.CharField(
        max_length=30,
        verbose_name=u'段名称',
        default=u'',
    )

    duanDesc = models.CharField(
        max_length=30,
        verbose_name=u'段描述',
        default=u'',
    )

    duanPrefix = models.CharField(
        max_length=30,
        verbose_name=u'段前缀',
        default=u'',
    )

    duanVersion = models.CharField(
        max_length=30,
        verbose_name=u'段版本',
        default=u'',
    )

    duanType = models.CharField(
        max_length=30,
        verbose_name=u'段类型',
        default=u'',
    )

    cpuType = models.CharField(
        max_length=30,
        verbose_name=u'cpu架构类型',
        default=u'',
    )

    developUnit = models.CharField(
        max_length=30,
        verbose_name=u'研发单位',
        default=u'',
    )

    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )

    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )

    isLatest = models.BooleanField(
        verbose_name=u'是否为最新版段',
        default=False,
    )

    def __unicode__(self):
        return self.createTime

    class Meta:
        ordering = ('createTime',)


class PushOther(models.Model):
    PUSHSTATE = (
        ('0', u'推送中'),
        ('1', u'推送失败'),
        ('2', u'推送成功'),
    )

    duan = models.ForeignKey(
        DuanInfo,
        verbose_name=u"段",
        on_delete=models.CASCADE,
        related_name='pushOther_duan',
        null=True
    )
    pushState = models.CharField(
        max_length=10,
        verbose_name=u'推送状态',
        choices=PUSHSTATE,
        default=u'0',
    )
    msg = models.CharField(
        max_length=100,
        verbose_name=u'推送信息',
        default=u'',
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )

    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )

    def __unicode__(self):
        return self.createTime

    class Meta:
        ordering = ('createTime',)
